package com.dev.security.manager.controllers;

import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.security.manager.beans.Account;
import com.dev.security.manager.service.IAccountService;
import com.dev.security.manager.service.IRoleService;
import com.dev.security.util.DataGrid;
import com.dev.security.util.Response;

/**
 * 账户管理
 * @author 潜行的青衣
 * @see
 */
@Controller
@RequestMapping("/manager/account/")
public class AccountController {

	@Autowired
	private IAccountService accountService;
	
	@Autowired
	private PasswordEncoder passwordEncoder;
	
	@Autowired
	private IRoleService roleService;
	
	@RequestMapping("browse")
	public @ResponseBody DataGrid<Account> browse(Account account, int page, int rows) {
		Sort sort = new Sort(new Order(Direction.ASC, "createtime"));
		Page<Account> pager = this.accountService.findPager(account, page, rows, sort);
		return new DataGrid<Account>(pager);
	}
	
	@RequestMapping("create")
	public @ResponseBody Response create(Account account) {
		account.setCreatetime(DateFormatUtils.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"));
		account.setPassword(this.passwordEncoder.encode(account.getPassword()));
		return new Response(this.accountService.create(account), account);
	}
	
	@RequestMapping("update")
	public @ResponseBody Response update(Account account) {
		Account old = this.accountService.findAccount(account.getId());
		account.setCreatetime(old.getCreatetime());
		//是否修改密码
		if(StringUtils.isEmpty(account.getPassword())) {
			account.setPassword(old.getPassword());
		} else {
			account.setPassword(this.passwordEncoder.encode(account.getPassword()));
		}
		return new Response(this.accountService.update(account), account);
	}
	
	@RequestMapping("remove")
	public @ResponseBody Response remove(String[] ids) {
		this.accountService.remove(ids);
		return new Response(true);
	}
	
	@RequestMapping("roles")
	public @ResponseBody Map<String, Object> roles(String id) {
		Map<String, Object> result = new LinkedHashMap<String, Object>();
		result.put("roles", roleService.findAll());
		result.put("own", this.accountService.findRoleIds(StringUtils.trimToEmpty(id)));
		return result;
	}
	
	@RequestMapping("alloc")
	public @ResponseBody Response alloc(String id, String[] role_ids) {
		this.accountService.allocRoles(id, role_ids);
		return new Response(true);
	}
}
